$(document).ready(function(){
	$("#form").validate({
		rules: {
			title: "required",
			description:"required",
			type: "required",
			ifsys:"required"
		},
		messages: {
			title: "标题不能为空！",
			description:"描述不能为空！",
			type: "请选择类型！",
			ifsys:"请选择权限"
		},
		submitHandler: function(form) {
			var method = $('#method').val();
			if(method=="save"){
				$(form).ajaxSubmit({
					success:function(data){
						var fun = data.funMethod.split(';')[0];
						if(fun=="add"){
							alert('添加成功');
							var id = data.funMethod.split(';')[1];
							var title = $('#title').val();
							var parentid = $('#parentid').val();
							var left = $(window.parent.frames['functionleftFrame'].document);
							var li = left.find("#"+parentid)||left.find('.root');
							if(li.find('ul').length>0){
								li.find('ul').append('<li class="line"></li><li id="'+id+'" class="doc-last"><a target="functionMainFrame" href="findFunctionAction!modifyFunction.action?functionid='+id+'"><span>'+title+'</span></a></li>');
							}else{
								li.attr("class","folder-open");
								li.append('<ul><li class="line"/><li id="'+id+'" class="doc-last"><a target="functionMainFrame" href="findFunctionAction!modifyFunction.action?functionid='+id+'"><span>'+title+'</span></a></li><li class="line-last"></li></ul>');
							}
							$('#functionid').val(id);
						}else if(fun=="update"){
							alert('修改成功');
							var id = $('#functionid').val();
							var title = $('#title').val();
							var li = $(window.parent.frames['functionleftFrame'].document).find("#"+id);
							li.find('a span').eq(0).html(title);
						}
					}
				});
			}else if(method=="add"){
				form.submit();
			}else if(method=="del"){
				$(form).ajaxSubmit({
					success:function(data){
						if(data.funMethod=="success"){
							alert('删除成功');	
							var id = $('#functionid').val();
							var li = $(window.parent.frames['functionleftFrame'].document).find("#"+id);
							var sib = $(li+'+li');
							sib.remove();
							li.remove();
							$('#functionid').val('');
						}else{
							alert('不能删除或已删除');
						}
					},
					error:function(){
						alert('删除失败或没有此记录');
					}
				});
			}
		}
	});
	
	$('#save').click(function(){
		$('#method').val('save');
		$('form').attr('action','ajaxFunctionAction!saveFunction.action').submit();
	});
	$('#add').click(function(){
		$('#method').val('add');
		$('form').attr('action','findFunctionAction!addSubFunction.action').submit();
	});
	$('#del').click(function(){
		$('#method').val('del');
		if($('#functionid').val==""){
			alert('已删除！');
		}else{
			$('form').attr('action','ajaxFunctionAction!delFunction.action').submit();
		}
	});
});